package com.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.model.DifficultyInfo;
import com.model.Paper;
import com.model.Programming;
import com.model.RankInfo;
import com.model.Student;
import com.model.User;
import com.service.ExamService;
import com.service.PaperService;
import com.util.BaseAction;
import com.util.cheResult;

public class ExamAction extends BaseAction {
    public String getPapername() {
        return papername;
    }

    public void setPapername(String papername) {

        try {
            this.papername = java.net.URLDecoder.decode(papername, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    private static final long serialVersionUID = 5353252383210744814L;

    public List<?> getDatalist() {
        return datalist;
    }

    public void setDatalist(List<?> datalist) {
        this.datalist = datalist;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public cheResult getCheckResult() {
        return checkResult;
    }

    public void setCheckResult(cheResult checkResult) {
        this.checkResult = checkResult;
    }

    public List<RankInfo> getRankInfoList() {
        return rankInfoList;
    }

    public void setRankInfoList(List<RankInfo> rankInfoList) {
        this.rankInfoList = rankInfoList;
    }

    public String getUanswer() {
        return Uanswer;
    }

    public void setUanswer(String uanswer) {
        Uanswer = uanswer;
    }

    public ExamService getExamService() {
        return ExamService;
    }

    public void setExamService(ExamService examService) {
        ExamService = examService;
    }

    public List<Programming> getProgramList() {
        return programList;
    }

    public void setProgramList(List<Programming> programList) {
        this.programList = programList;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getEndline() {
        return endline;
    }

    public void setEndline(String endline) {
        this.endline = endline;
    }

    public Paper getPaper() {
        return paper;
    }

    public void setPaper(Paper paper) {
        this.paper = paper;
    }

    public PaperService getPaperService() {
        return PaperService;
    }

    @Resource
    public void setPaperService(PaperService paperService) {
        PaperService = paperService;
    }

    private PaperService PaperService;
    @Resource
    private ExamService ExamService;
    private int id;
    private int uid;
    private Paper paper;
    private List<Programming> programList;
    private String Uanswer;
    private cheResult checkResult; // 用于接受评判结果 并用json传回前端
    private int type; // 试卷类型 java0 or c1
    private List<?> datalist;
    private String answer;
    private String papername;
    private String endline;
    private String beginline;

    private List<RankInfo> rankInfoList;
    private List<DifficultyInfo> difficultyInfoList;

    public void getPaperById() {
        paper = PaperService.findById(id);

    }

    public String beginExam() {
        if (session.get("loginUser") != null) {
            User u = (User) session.get("loginUser");
            String flag = "user";
            programList = ExamService.getProgramList(id, u.getId(), flag);
        } else {
            Student stu = (Student) session.get("student");
            String flag = "student";
            programList = ExamService.getProgramList(id, stu.getId(), flag);
        }
        // programList = ExamService.getProgramList(id);
        paper = PaperService.findById(id);
        if (paper.getPaperpw() != null) {
            return "beginPW";
        }
        setEndline(paper.getEndtime().toString());
        setBeginline(paper.getBegintime().toString());
        session.put("paper", paper);
        return "begin";
    }

    public String pwbeginExam() {
        if (session.get("loginUser") != null) {
            User u = (User) session.get("loginUser");
            String flag = "user";
            programList = ExamService.getProgramList(id, u.getId(), flag);
        } else {
            Student stu = (Student) session.get("student");
            String flag = "student";
            programList = ExamService.getProgramList(id, stu.getId(), flag);
        }
        // programList = ExamService.getProgramList(id);

        paper = PaperService.findById(id);
        setEndline(paper.getEndtime().toString());
        setBeginline(paper.getBegintime().toString());
        session.put("paper", paper);
        return "pwbegin";
    }

    public String checkAnswer() throws IOException {
        response.setContentType("text/html;charset=UTF-8.");
        response.setCharacterEncoding("utf-8");
        checkResult = new cheResult();
        List<?> list = new ArrayList<Object>();

        System.out.println("id++++++++++++++++++++++++++++++++++++++++++++++++++++++" + id);
        System.out.println("type++++++++++++++++++++++++++++++++++++++++++++++++++++" + type);
        System.out.println("answer++++++++++++++++++++++++++++++++++++++++++++++++++" + answer);
        Paper paper = (Paper) session.get("paper");
        User user = (User) session.get("loginUser");

        // User表示注册用户，Student表示班级用户
        if (session.get("isclass").equals("no")) {
            list = ExamService.checkUser(id, answer, type, paper, user);// 检查注册用户提交的代码
        } else {
            Student stu = (Student) session.get("student");
            list = ExamService.checkStudent(id, answer, type, paper, stu);// 检查班级用户提交的代码
        }

        checkResult.setId(list.get(0).toString());
        checkResult.setResult(list.get(1).toString());
        checkResult.setScore((Integer) list.get(2));
        checkResult.setTime((String) list.get(3));

        return SUCCESS;
    }

    public String paperInfo() {
        String isclass = (String) session.get("isclass");
        int usid;
        String flag;
        if (session.get("role").equals("1")) {
            usid = uid;
            flag = "no";
        } else {
            if (isclass.equals("yes")) {
                Student u = (Student) session.get("student");
                usid = u.getId();
                flag = "yes";
            } else {
                User u = (User) session.get("loginUser");
                usid = u.getId();
                flag = "no";
            }
        }
        int pid = id;
        datalist = ExamService.paperInfo(usid, pid, flag);
        return "paperinfo";
    }

    public String showDifficulty() {
        datalist = ExamService.showDifficulty();
        User u = (User) session.get("loginUser");
        session.put("userrole", u.getRole());
        return "showDifficulty";
    }

    public String showRank() {
        datalist = ExamService.showRank();
        User u = (User) session.get("loginUser");
        session.put("userrole", u.getRole());
        return "showRank";
    }

    public String rankInfo() {
        setRankInfoList(ExamService.rankInfo(id));
        paper = PaperService.findById(id);
        setPapername(paper.getPapername());
        return "rankInfo";
    }

    public String difficultyInfo() {
        setDifficultyInfoList(ExamService.DifficultyInfo(id));
        paper = PaperService.findById(id);
        setPapername(paper.getPapername());
        return "difficultyInfo";
    }

    public List<DifficultyInfo> getDifficultyInfoList() {
        return difficultyInfoList;
    }

    public void setDifficultyInfoList(List<DifficultyInfo> difficultyInfoList) {
        this.difficultyInfoList = difficultyInfoList;
    }

	public String getBeginline() {
		return beginline;
	}

	public void setBeginline(String beginline) {
		this.beginline = beginline;
	}

}
